﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using Models;

namespace DAL
{
    public class WxPayAddDAL
    {
#pragma warning disable CS0618 // “ConfigurationSettings.AppSettings”已过时:“This method is obsolete, it has been replaced by System.Configuration!System.Configuration.ConfigurationManager.AppSettings”
        public static string conn = ConfigurationSettings.AppSettings["con"].ToString();
#pragma warning restore CS0618 // “ConfigurationSettings.AppSettings”已过时:“This method is obsolete, it has been replaced by System.Configuration!System.Configuration.ConfigurationManager.AppSettings”

        /// <summary>
        /// 将微信充值记录保存至jssj表
        /// </summary>
        /// <param name="openid"></param>
        /// <param name="money"></param>
        /// <param name="time"></param>
        /// <returns></returns>
        public static int addPayTolssj(string openid,double money,string time)
        {
            SqlParameter[] sp = new SqlParameter[]{
                new SqlParameter("@openid",openid),
                new SqlParameter("@money",money),
                new SqlParameter("@xfsj",time)
            };
            string i = SqlHelper.ExecuteScalar(conn,CommandType.StoredProcedure,"WxPayAdd",sp).ToString();
            return Int32.Parse(i);
        }
        /// <summary>
        /// 判断记录是否保存过
        /// </summary>
        /// <param name="openid"></param>
        /// <param name="time"></param>
        /// <returns></returns>
        public static int isExists(string openid,string time, string out_trade_no)
        {
            string sqlstr = "";
            SqlParameter[] sp = new SqlParameter[]{
                new SqlParameter("@openid",openid),
                new SqlParameter("@out_trade_no",out_trade_no),
                new SqlParameter("@xfsj",time)
            };
            if (getIsOnline() != 0)//如果是联机则查询lssj表
            {
                //sqlstr = "select * from lssj where xfsj = @xfsj and ryid = (select ryid from idbind where openid = @openid) and xffs='微信充值'";
                sqlstr = "select * from OrderNumber  where [Order]=@out_trade_no  and State=1";
            }
            else
            {
                sqlstr = "select * from lssj_outline where xfsj = @xfsj and ryid = (select ryid from idbind where openid = @openid) and xffs='微信充值'";
            }
            DataSet ds = SqlHelper.ExecuteDataSet(conn,CommandType.Text,sqlstr,sp);
            return ds.Tables[0].Rows.Count;
        }

        /// <summary>
        /// 添加扫码支付记录
        /// </summary>
        /// <param name="order_no"></param>
        /// <param name="cardno"></param>
        /// <param name="openid"></param>
        /// <param name="is_subscribe"></param>
        /// <param name="trade_type"></param>
        /// <param name="total_fee"></param>
        /// <param name="transaction_id"></param>
        /// <param name="time_end"></param>
        /// <returns></returns>
      
        public static int addNativeRecord(string order_no, string cardno, string openid, string is_subscribe, string trade_type, string total_fee, string transaction_id, string time_end)
        {
            SqlParameter[] sp = new SqlParameter[] {
                new SqlParameter("@order_no",order_no),
                new SqlParameter("@cardno",cardno),
                new SqlParameter("@openid",openid),
                new SqlParameter("@is_subscribe",is_subscribe),
                new SqlParameter("@trade_type",trade_type),
                new SqlParameter("@total_fee",total_fee),
                new SqlParameter("@transaction_id",transaction_id),
                new SqlParameter("@time_end",time_end)
            };
            string sqlstr = "if not exists(select cardno from native_record where transaction_id=@transaction_id) insert into native_record values(@order_no,@cardno,@openid,@is_subscribe,@trade_type,@total_fee,@transaction_id,@time_end,0)";
            return SqlHelper.ExecuteNonQuery(conn, CommandType.Text, sqlstr, sp);
        }
        public static int SlotCardPayRecord(SlotCardPay sc)
        {
            SqlParameter[] sp = new SqlParameter[] {
                new SqlParameter("@openid",sc.openid),
                new SqlParameter("@is_subscribe",sc.is_subscribe),
                new SqlParameter("@trade_type",sc.trade_type),
                new SqlParameter("@bank_type",sc.bank_type),
                new SqlParameter("@fee_type",sc.fee_type),
                new SqlParameter("@total_fee",sc.total_fee),
                new SqlParameter("@settlement_total_fee",sc.settlement_total_fee),
                new SqlParameter("@coupon_fee",sc.coupon_fee),
                new SqlParameter("@cash_fee_type",sc.cash_fee_type),
                new SqlParameter("@cash_fee",sc.cash_fee),
                new SqlParameter("@transaction_id",sc.transaction_id),
                new SqlParameter("@out_trade_no",sc.out_trade_no),
                new SqlParameter("@attach",sc.attachs),
                 new SqlParameter("@time_end",sc.time_end),
                  new SqlParameter("@promotion_detail",sc.promotion_detail)
            };
            string sqlstr = "insert into SlotCardPay values(@openid,@is_subscribe,@trade_type,@bank_type,@fee_type,@total_fee,@settlement_total_fee,@coupon_fee,@cash_fee_type,@cash_fee,@transaction_id,@out_trade_no,@attach,@time_end,@promotion_detail)";
            return SqlHelper.ExecuteNonQuery(conn, CommandType.Text, sqlstr, sp);
        }
        /// <summary>
        /// 判断是脱机还是联机
        /// </summary>
        /// <returns></returns>
        public static int getIsOnline()
        {
            SqlParameter[] sp = new SqlParameter[]{
            };
            string sqlstr = "select isonline from systemset";
            string s = SqlHelper.ExecuteScalar(conn, CommandType.Text, sqlstr, sp).ToString();
            return Int32.Parse(s);
        }


        public static DataTable getWxPayInfo()
        {
            SqlParameter[] sp = new SqlParameter[]{
            };
            string sqlstr = "select * from wxpayInfoSet";
            return SqlHelper.ExecuteDataSet(conn, CommandType.Text, sqlstr, sp).Tables[0];
        }

        public static int setWxPayInfo(WxPayInfo wp)
        {
            SqlParameter[] sp = new SqlParameter[]{
                new SqlParameter("@CompanyName",wp.companyName),
                new SqlParameter("@body",wp.body),
                new SqlParameter("@product_id",wp.product_id),
                new SqlParameter("@merchantNum",wp.merchantNum),
                new SqlParameter("@payKey",wp.payKey)
            };
            string sqlstr = "if not exists(select 1 from wxPayInfoSet) begin insert into wxpayInfoSet values(@CompanyName,@body,@product_id,@merchantNum,@payKey) end else begin update wxpayInfoSet set CompanyName=@CompanyName,body=@body,product_id=@product_id,merchantNum=@merchantNum,payKey=@payKey end";
            return SqlHelper.ExecuteNonQuery(conn, CommandType.Text, sqlstr, sp);
        }

        public static DataTable getAliPayInfo()
        {
            SqlParameter[] sp = new SqlParameter[]{
            };
            string sqlstr = "select * from alipayInfoSet";
            return SqlHelper.ExecuteDataSet(conn, CommandType.Text, sqlstr, sp).Tables[0];
        }

        public static int setAliPayInfo(AlipayInfo ap)
        {
            SqlParameter[] sp = new SqlParameter[]{
                new SqlParameter("@alipay_public_key",ap.alipay_public_key),
                new SqlParameter("@merchant_private_key",ap.merchant_private_key),
                new SqlParameter("@merchant_public_key",ap.merchant_public_key),
                new SqlParameter("@appId",ap.appId),
                new SqlParameter("@pid",ap.pid)
            };
            string sqlstr = "if not exists(select 1 from aliPayInfoSet) begin insert into alipayInfoSet values(@alipay_public_key,@merchant_private_key,@merchant_public_key,@appId,@pid) end else begin update AlipayInfoSet set alipay_public_key=@alipay_public_key,merchant_private_key=@merchant_private_key,merchant_public_key=@merchant_public_key,appId=@appId,pid=@pid end";
            return SqlHelper.ExecuteNonQuery(conn, CommandType.Text, sqlstr, sp);
        }
    }
}
